`include "ascon_define.v"

module `ASCON_AEAD_DE_TOP
(
    input                                        ascon_aead_clk_i,
    input                                        ascon_aead_rst_n_i,
//与interface相连
    input              [`AHB_BUS_ADDR_W - 1 : 0] haddr_i,
    input                           [4  - 1 : 0] hprot_i,
    input                           [1  - 1 : 0] hsel_i,
    input                           [3  - 1 : 0] hsize_i,
    input                           [2  - 1 : 0] htrans_i,
    input              [`AHB_BUS_DATA_W - 1 : 0] hwdata_i,
    input                           [1  - 1 : 0] hwrite_i,
    output             [`AHB_BUS_DATA_W - 1 : 0] hrdata_o,
    output                          [1  - 1 : 0] hready_o,
    output                          [2  - 1 : 0] hresp_o,
    output                          [1  - 1 : 0] intr_o
);

wire                   [`AHB_BUS_ADDR_W - 1 : 0] if2tx_buf_raddr;
wire                   [`AHB_BUS_DATA_W - 1 : 0] tx_buf2if_rdat;

wire                                             if2rx_buf_en;
wire                                             if2rx_buf_wen;
wire                   [`AHB_BUS_ADDR_W - 1 : 0] if2rx_buf_waddr;
wire                   [`AHB_BUS_DATA_W - 1 : 0] if2rx_buf_wdat;

wire                                             if2ctrl_regs_en;
wire                                             if2ctrl_regs_wen;
wire                   [`AHB_BUS_ADDR_W - 1 : 0] if2ctrl_regs_addr;
wire                   [`AHB_BUS_DATA_W - 1 : 0] if2ctrl_regs_wdata;
wire                   [`AHB_BUS_DATA_W - 1 : 0] ctrl_regs2if_rdata;

wire                                             fsm2ctrl_regs_prot;
wire                                             fsm2ctrl_regs_fsm_sta_wen;
wire                                             fsm2ctrl_regs_fsm_en_wen;
wire                                             fsm2ctrl_regs_w_t_wen;
wire                           [`CTRL_REG_W-1:0] fsm2ctrl_regs_w_sta;
wire                           [`CTRL_REG_W-1:0] fsm2ctrl_regs_w_en;
wire                                  [`T_W-1:0] fin2ctrl_regs_w_t;

wire                           [`CTRL_REG_W-1:0] ctrl_regs2fsm_msg_block_num;
wire                           [`CTRL_REG_W-1:0] ctrl_regs2fsm_msg_addr;
wire                           [`CTRL_REG_W-1:0] ctrl_regs2fsm_aead_ctrl_sta;
wire                                             ctrl_regs2fsm_aead_ctrl_en;
wire                                             ctrl_regs2fsm_aead_ctrl_mode;
wire                           [`CTRL_REG_W-1:0] ctrl_regs2fsm_aead_c_addr;

wire                                  [`A_W-1:0] ctrl_regs2ad_aead_a;
wire                                  [`K_W-1:0] ctrl_regs2init2fin_aead_k;
wire                                  [`N_W-1:0] ctrl_regs2init_aead_n;

wire                                             fsm2tx_en;
wire                                             fsm2tx_wen;
wire                     [`BUF_ADDR_W+1 - 1 : 0] fsm2tx_addr;
wire                              [`C_W - 1 : 0] ct2tx_wdata;

wire                         [`BUF_ADDR_W+1-1:0] fsm2rx_buf_raddr;
wire                                  [`P_W-1:0] rx_buf2ct_rdata;

wire                                             init2fsm_vld;
wire                                             fsm2init_en;
wire                                             fsm2init_vld;
wire                                             fsm2init_mode;

wire                                             ad2fsm_vld;
wire                                             fsm2ad_vld;
wire                                             fsm2ad_en;
wire                                             fsm2ad_mode;

wire                                             ct2fsm_vld;
wire                                             fsm2ct_vld;
wire                                             fsm2ct_en;
wire                                             fsm2ct_mode;
wire                                             fsm2ct_first;
wire                                             fsm2ct_last;

wire                                             final2fsm_vld;
wire                                             fsm2final_vld;
wire                                             fsm2final_en;
wire                                             fsm2final_mode;

wire                                  [`S_W-1:0] init2ad_s;
wire                                  [`S_W-1:0] ad2ct_s;
wire                                  [`S_W-1:0] ct2fin_s;

`ASCON_AEAD_AHB_IF
u_ascon_aead_ahb_if
    (
    //ahb总线信号
    .tai_clk_i                         (ascon_aead_clk_i                       ),
    .tai_rst_n_i                       (ascon_aead_rst_n_i                     ),
    .tai_haddr_i                       (haddr_i                                ),
    .tai_hprot_i                       (hprot_i                                ),
    .tai_hsel_i                        (hsel_i                                 ),
    .tai_hsize_i                       (hsize_i                                ),
    .tai_htrans_i                      (htrans_i                               ),
    .tai_hwdata_i                      (hwdata_i                               ),
    .tai_hwrite_i                      (hwrite_i                               ),

    .tai_hrdata_o                      (hrdata_o                               ),
    .tai_hready_o                      (hready_o                               ),
    .tai_hresp_o                       (hresp_o                                ),
    .tai_t2r_intr_o                    (intr_o                                 ),
    //tx buf信号
    .tx_buf_raddr_o                    (if2tx_buf_raddr                        ),
    .tx_buf_rdat_i                     (tx_buf2if_rdat                         ),

    //rx buf信号
    .rx_buf_en_o                       (if2rx_buf_en                           ),
    .rx_buf_wen_o                      (if2rx_buf_wen                          ),
    .rx_buf_waddr_o                    (if2rx_buf_waddr                        ),
    .rx_buf_wdat_o                     (if2rx_buf_wdat                         ),

    //ctrl regs信号
    .ctrl_regs_en_o                    (if2ctrl_regs_en                        ),
    .ctrl_regs_wen_o                   (if2ctrl_regs_wen                       ),
    .ctrl_regs_addr_o                  (if2ctrl_regs_addr                      ),
    .ctrl_regs_wdata_o                 (if2ctrl_regs_wdata                     ),
    .ctrl_regs_rdata_i                 (ctrl_regs2if_rdata                     )
    );

`ASCON_AEAD_CTRL_REGS
u_ascon_aead_ctrl_regs
(
    .ascon_aead_clk_i                  (ascon_aead_clk_i                       ),
    .ascon_aead_rst_n_i                (ascon_aead_rst_n_i                     ),
//总线访问信号
    .ascon_aead_ctrl_regs_en_i         (if2ctrl_regs_en                        ),
    .ascon_aead_ctrl_regs_wen_i        (if2ctrl_regs_wen                       ),
    .ascon_aead_ctrl_regs_addr_i       (if2ctrl_regs_addr                      ),
    .ascon_aead_ctrl_regs_wdata_i      (if2ctrl_regs_wdata                     ),
    .ascon_aead_ctrl_regs_rdata_o      (ctrl_regs2if_rdata                     ),
//状态机访问信号
    .ascon_aead_ctrl_regs_prot_i       (fsm2ctrl_regs_prot                     ),
    .ascon_aead_ctrl_regs_fsm_sta_wen_i(fsm2ctrl_regs_fsm_sta_wen              ),
    .ascon_aead_ctrl_regs_fsm_en_wen_i (fsm2ctrl_regs_fsm_en_wen               ),
    .ascon_aead_ctrl_regs_w_t_wen_i    (fsm2ctrl_regs_w_t_wen                  ),
    .ascon_aead_ctrl_regs_w_sta_i      (fsm2ctrl_regs_w_sta                    ),
    .ascon_aead_ctrl_regs_w_en_i       (32'h0000_0000                          ),
    .ascon_aead_ctrl_regs_msg_block_num_o (ctrl_regs2fsm_msg_block_num         ),
    .ascon_aead_ctrl_regs_msg_addr_o      (ctrl_regs2fsm_msg_addr              ),
    .ascon_aead_ctrl_regs_aead_ctrl_sta_o (ctrl_regs2fsm_aead_ctrl_sta         ),
    .ascon_aead_ctrl_regs_aead_ctrl_en_o  (ctrl_regs2fsm_aead_ctrl_en          ),
    .ascon_aead_ctrl_regs_aead_ctrl_mode_o(ctrl_regs2fsm_aead_ctrl_mode        ),
    .ascon_aead_ctrl_regs_aead_c_addr_o   (ctrl_regs2fsm_aead_c_addr           ),
//与fin相连
    .ascon_aead_ctrl_regs_w_t_i        (fin2ctrl_regs_w_t                      ),
//输出配置信号
    .ascon_aead_ctrl_regs_aead_a_o     (ctrl_regs2ad_aead_a                    ),
    .ascon_aead_ctrl_regs_aead_k_o     (ctrl_regs2init2fin_aead_k              ),
    .ascon_aead_ctrl_regs_aead_n_o     (ctrl_regs2init_aead_n                  )
);

`TX_BUF_4_32B_64
u_tx_buf_4_32b_64
    (
    .clk_i                             (ascon_aead_clk_i                       ),
    .rstn_i                            (ascon_aead_rst_n_i                     ),

    .en_i                              (fsm2tx_en                              ),
    .wen_i                             (fsm2tx_wen                             ),
    .waddr_i                           (fsm2tx_addr                            ),
    .wdata_i                           (ct2tx_wdata                            ),
    .mode_i                            (ctrl_regs2fsm_aead_ctrl_mode           ),

    .raddr_i                           (if2tx_buf_raddr                        ),
    .rdata_o                           (tx_buf2if_rdat                         )
    );

`RX_BUF_4_32B_64
u_rx_buf_4_32b_64
    (
    .clk_i                             (ascon_aead_clk_i                       ),
    .rstn_i                            (ascon_aead_rst_n_i                     ),

    .en_i                              (if2rx_buf_en                           ),
    .wen_i                             (if2rx_buf_wen                          ),
    .waddr_i                           (if2rx_buf_waddr                        ),
    .wdata_i                           (if2rx_buf_wdat                         ),
    .mode_i                            (ctrl_regs2fsm_aead_ctrl_mode           ),

    .raddr_i                           (fsm2rx_buf_raddr                       ),
    .rdata_o                           (rx_buf2ct_rdata                        )
    );

`ASCON_AEAD_DE_CTRL
u_ascon_aead_de_ctrl
(
    .ascon_aead_clk_i                  (ascon_aead_clk_i                       ),
    .ascon_aead_rst_n_i                (ascon_aead_rst_n_i                     ),
//与ctrl regs相连的信号
    .ascon_aead_ctrl_msg_block_num_i   (ctrl_regs2fsm_msg_block_num            ),
    .ascon_aead_ctrl_msg_addr_i        (ctrl_regs2fsm_msg_addr                 ),
    .ascon_aead_ctrl_aead_ctrl_sta_i   (ctrl_regs2fsm_aead_ctrl_sta            ),
    .ascon_aead_ctrl_aead_ctrl_en_i    (ctrl_regs2fsm_aead_ctrl_en             ),
    .ascon_aead_ctrl_aead_ctrl_mode_i  (ctrl_regs2fsm_aead_ctrl_mode           ),
    .ascon_aead_ctrl_aead_c_addr_i     (ctrl_regs2fsm_aead_c_addr              ),
    .ascon_aead_ctrl_prot_o            (fsm2ctrl_regs_prot                     ),
    .ascon_aead_ctrl_fsm_sta_wen_o     (fsm2ctrl_regs_fsm_sta_wen              ),
    .ascon_aead_ctrl_fsm_en_wen_o      (fsm2ctrl_regs_fsm_en_wen               ),
    .ascon_aead_ctrl_fsm_t_wen_o       (fsm2ctrl_regs_w_t_wen                  ),
    .ascon_aead_ctrl_w_sta_o           (fsm2ctrl_regs_w_sta                    ),
    .ascon_aead_ctrl_w_en_o            (fsm2ctrl_regs_w_en                     ),
//与ascon_aead_init连接
    .ascon_aead_ctrl_init_vld_i        (init2fsm_vld                           ),
    .ascon_aead_ctrl_init_en_o         (fsm2init_en                            ),
    .ascon_aead_ctrl_init_vld_o        (fsm2init_vld                           ),
    .ascon_aead_ctrl_init_mode_o       (fsm2init_mode                          ),
//与ascon_aead_ad连接
    .ascon_aead_ctrl_ad_vld_i          (ad2fsm_vld                             ),
    .ascon_aead_ctrl_ad_vld_o          (fsm2ad_vld                             ),
    .ascon_aead_ctrl_ad_en_o           (fsm2ad_en                              ),
    .ascon_aead_ctrl_ad_mode_o         (fsm2ad_mode                            ),
//与ascon_aead_ct连接
    .ascon_aead_ctrl_ct_vld_i          (ct2fsm_vld                             ),
    .ascon_aead_ctrl_ct_vld_o          (fsm2ct_vld                             ),
    .ascon_aead_ctrl_ct_en_o           (fsm2ct_en                              ),
    .ascon_aead_ctrl_ct_mode_o         (fsm2ct_mode                            ),
    .ascon_aead_ctrl_ct_first_o        (fsm2ct_first                           ),
    .ascon_aead_ctrl_ct_last_o         (fsm2ct_last                            ),
//与ascon_aead_final连接
    .ascon_aead_ctrl_final_vld_i       (final2fsm_vld                          ),
    .ascon_aead_ctrl_final_vld_o       (fsm2final_vld                          ),
    .ascon_aead_ctrl_final_en_o        (fsm2final_en                           ),
    .ascon_aead_ctrl_final_mode_o      (fsm2final_mode                         ),
//与rx buf连接
    .ascon_aead_ctrl_rx_buf_addr_o     (fsm2rx_buf_raddr                       ),
//与tx buf连接
    .ascon_aead_ctrl_tx_buf_en_o       (fsm2tx_en                              ),
    .ascon_aead_ctrl_tx_buf_wen_o      (fsm2tx_wen                             ),
    .ascon_aead_ctrl_tx_buf_addr_o     (fsm2tx_addr                            )
);

`ASCON_AEAD_INIT
u_ascon_aead_init
(
    .ascon_aead_clk_i                  (ascon_aead_clk_i                       ),
    .ascon_aead_rst_n_i                (ascon_aead_rst_n_i                     ),

    .ascon_aead_init_en_i              (fsm2init_en                            ),
    .ascon_aead_init_vld_i             (fsm2init_vld                           ),
    .ascon_aead_init_mode_i            (fsm2init_mode                          ),
    .ascon_aead_init_n_i               (ctrl_regs2init_aead_n                  ),
    .ascon_aead_init_k_i               (ctrl_regs2init2fin_aead_k              ),

    .ascon_aead_init_s_o               (init2ad_s                              ),
    .ascon_aead_init_vld_o             (init2fsm_vld                           )
);

`ASCON_AEAD_AD
u_ascon_aead_ad
(
    .ascon_aead_clk_i                  (ascon_aead_clk_i                       ),
    .ascon_aead_rst_n_i                (ascon_aead_rst_n_i                     ),

    .ascon_aead_ad_vld_i               (fsm2ad_vld                             ),
    .ascon_aead_ad_en_i                (fsm2ad_en                              ),
    .ascon_aead_ad_mode_i              (fsm2ad_mode                            ),
    .ascon_aead_ad_a_i                 (ctrl_regs2ad_aead_a                    ),
    .ascon_aead_ad_s_i                 (init2ad_s                              ),

    .ascon_aead_ad_s_o                 (ad2ct_s                                ),
    .ascon_aead_ad_vld_o               (ad2fsm_vld                             )
);

`ASCON_AEAD_CT
u_ascon_aead_ct
(
    .ascon_aead_clk_i                  (ascon_aead_clk_i                       ),
    .ascon_aead_rst_n_i                (ascon_aead_rst_n_i                     ),

    .ascon_aead_ct_vld_i               (fsm2ct_vld                             ),
    .ascon_aead_ct_en_i                (fsm2ct_en                              ),
    .ascon_aead_ct_mode_i              (fsm2ct_mode                            ),
    .ascon_aead_ct_first_i             (fsm2ct_first                           ),
    .ascon_aead_ct_last_i              (fsm2ct_last                            ),
    .ascon_aead_ct_s_i                 (ad2ct_s                                ),
    .ascon_aead_ct_c_i                 (rx_buf2ct_rdata                        ),

    .ascon_aead_ct_s_o                 (ct2fin_s                               ),
    .ascon_aead_ct_vld_o               (ct2fsm_vld                             ),
    .ascon_aead_ct_p_o                 (ct2tx_wdata                            )
);

`ASCON_AEAD_FINAL
u_ascon_aead_final
(
    .ascon_aead_clk_i                  (ascon_aead_clk_i                       ),
    .ascon_aead_rst_n_i                (ascon_aead_rst_n_i                     ),

    .ascon_aead_final_vld_i            (fsm2final_vld                          ),
    .ascon_aead_final_en_i             (fsm2final_en                           ),
    .ascon_aead_final_mode_i           (fsm2final_mode                         ),
    .ascon_aead_final_k_i              (ctrl_regs2init2fin_aead_k              ),
    .ascon_aead_final_s_i              (ct2fin_s                               ),

    .ascon_aead_final_t_o              (fin2ctrl_regs_w_t                      ),
    .ascon_aead_final_vld_o            (final2fsm_vld                          )
);



endmodule